﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RFID信息初始化工作台
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if(!listView_File.Enabled)
            {
                MessageBox.Show("请先修改或则取消修改后再添加!");
                return;
            }

            if (txt_User.Text.Trim() == "" || txt_Pwd.Text.Trim() == "" || txt_RName.Text.Trim() == "")
                MessageBox.Show("请输入完整!");
            else
            {
                //YDRFIDEntities conn = new YDRFIDEntities();
                //T_USER en = new T_USER();
                //en.U_NAME = txt_User.Text;
                //en.U_PWD = txt_Pwd.Text;
                //en.U_RNAME = txt_RName.Text;
                //if (conn.T_USER.Select(c => c).Where(c => c.U_NAME == en.U_NAME).Count() > 0)
                //    MessageBox.Show("用户名重复!");
                //else
                //{
                //    try
                //    {
                //        conn.T_USER.Add(en);
                //        conn.SaveChanges();
                //        ListViewItem item = new ListViewItem(new string[] { (listView_File.Items.Count + 1).ToString(), en.U_NAME, en.U_PWD, en.U_RNAME });
                //        item.Tag = en;
                //        listView_File.Items.Add(item);
                //        txt_User.Text = txt_Pwd.Text = txt_RName.Text = "";
                //        _id = 0;
                //    }
                //    catch { MessageBox.Show("添加失败!"); }
                //}
            }
        }

        int _id = 0;
        private void btn_Modify_Click(object sender, EventArgs e)
        {
            if (listView_File.Enabled || _id == 0)
                MessageBox.Show("请先双击要修改的用户!");
            else
            {
                if(txt_User.Text.Trim() == "" || txt_Pwd.Text.Trim() == "" || txt_RName.Text.Trim() == "")
                    MessageBox.Show("请输入完整!");
            else
            {
                //    YDRFIDEntities conn = new YDRFIDEntities();
                //    if (conn.T_USER.Select(c => c).Where(c => c.U_NAME == txt_User.Text && c.U_ID != _id).Count() > 0)
                //        MessageBox.Show("用户名重复!");
                //    else
                //    {
                //        try
                //        {
                //            var en = conn.T_USER.Select(c => c).Where(c => c.U_ID == _id).Single();
                //            //foreach(var en in query)
                //            {
                //                en.U_NAME = txt_User.Text;
                //                en.U_PWD = txt_Pwd.Text;
                //                en.U_RNAME = txt_RName.Text;
                //            }
                //            conn.SaveChanges();
                //            listView_File.SelectedItems[0].SubItems[1].Text = en.U_NAME;
                //            listView_File.SelectedItems[0].SubItems[2].Text = en.U_PWD;
                //            listView_File.SelectedItems[0].SubItems[3].Text = en.U_RNAME;
                //            listView_File.SelectedItems[0].Tag = en;
                //            txt_User.Text = txt_Pwd.Text = txt_RName.Text = "";
                //            _id = 0;
                //            listView_File.Enabled = true;
                //        }
                //        catch { MessageBox.Show("修改失败!"); }
                //    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!listView_File.Enabled)
            {
                listView_File.Enabled = true;
                _id = 0;
            }
        }

        private void listView_File_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //if(listView_File.SelectedItems.Count == 1)
            //{
            //    var n = listView_File.SelectedItems[0].Tag as T_USER;
            //    _id = n.U_ID;
            //    txt_Pwd.Text = n.U_PWD;
            //    txt_RName.Text = n.U_RNAME;
            //    txt_User.Text = n.U_NAME;
            //    listView_File.Enabled = false;
            //}
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            //YDRFIDEntities conn = new YDRFIDEntities();
            //var query = conn.T_USER.Select(c => c).Where(c => c.U_NAME != "admin").ToList();
            //foreach(var en in query)
            //{
            //    ListViewItem item = new ListViewItem(new string[] { (listView_File.Items.Count + 1).ToString(), en.U_NAME, en.U_PWD, en.U_RNAME });
            //    item.Tag = en;
            //    listView_File.Items.Add(item);
            //}
          
        }
    }
}
